package com.example.edu.Mapper;

import com.example.edu.Dto.*;
import com.example.edu.Entity.Document;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DocumentMapper {
    @Insert("insert into document(document_name, document_type, document_user, document_course, document_chapter, `right`, is_outline, is_plan) VALUES " +
            "(#{document.document_name},#{document.document_type},#{document.document_user},#{document.document_course},#{document.document_chapter},#{document.right},#{document.is_outline},#{document.is_plan})")
    @Options(useGeneratedKeys = true, keyProperty = "document_id")
    void addDocument(@Param("document") Document document);

    @Select("select document_id from document")
    List<Integer> getIds();

    @Select("select course_id,course_name,chapter_name,document_type from course_chapter_doc where document_id = #{document_id}")
    ChatMessageDto getMessageByDocumentId(@Param("document_id") int document_id);

    @Select("select course_id,course_name,chapter_name from course_chapter_doc where document_id = #{document_id}")
    ChatMessageDto getMessageByDocumentId2(@Param("document_id") int document_id);

    @Select("select document_type,document_course from document where document_id = #{document_id}")
    Document getTypeAndCourse(@Param("document_id") int document_id);

    @Select("select document_user,document_course from document where document_id = #{document_id}")
    Document getAuthorAndCourse(@Param("document_id") int document_id);

    @Select("select document_course from document where document_id = #{document_id}")
    int getCourse(@Param("document_id") int document_id);

    @Select("select document_name,document_type,`right`,create_time,document_user,nickname,`like`,collect,view from doc_user where document_id = #{document_id}")
    DocumentListDtoR getDocumentMessage(@Param("document_id") int document_id);

    List<Document> getESDocumentMessage(@Param("ids") List<Integer> document_ids);

    @Select("select count(*) from document where document_course = #{course_id} and document_chapter = #{chapter_id}")
    int getDocumentNum(@Param("course_id") int course_id, @Param("chapter_id") int chapter_id);

    @Select("select count(*) from document where document_course = #{course_id} and document_chapter = #{chapter_id} and (`right` = 0 or document_user = #{user_id})")
    int getDocumentNumS(@Param("course_id") int course_id, @Param("chapter_id") int chapter_id, @Param("user_id") int user_id);

    @Select("select document_id,document_name,document_type,`right`,create_time,nickname from doc_user where document_course = #{course_id} and document_chapter = #{chapter_id} order by document_id desc " +
            "limit #{count} offset #{start}")
    List<DocumentListDtoR> getDocuments(@Param("course_id") int course_id, @Param("chapter_id") int chapter_id, @Param("start") int start, @Param("count") int count);

    @Select("select document_id,document_name,document_type,`right`,create_time,document_user,nickname from doc_user where document_course = #{course_id} and document_chapter = #{chapter_id} and (`right` = 0 or document_user = #{user_id}) order by document_id desc " +
            "limit #{count} offset #{start}")
    List<DocumentListDtoR> getDocumentsS(@Param("course_id") int course_id, @Param("chapter_id") int chapter_id, @Param("user_id") int user_id,@Param("start") int start, @Param("count") int count);

    List<DocumentListDtoR> getDocuments2(@Param("course_id") int course_id, @Param("chapter_id") int chapter_id, @Param("option") OptionDto option);

    List<DocumentListDtoR> getDocuments2S(@Param("course_id") int course_id, @Param("chapter_id") int chapter_id, @Param("option") OptionDto option, @Param("user_id") int user_id);

    @Select("select sum(`like`) as likes,sum(collect) as collects,sum(view) as views from document where document_user = #{id} and document_course = #{course_id}")
    LikeCollectViewDtoR getData(@Param("id") int id, @Param("course_id") int course_id);

    @Select("select sum(`like`) as likes,sum(collect) as collects,sum(view) as views from document where document_user = #{id}")
    LikeCollectViewDtoR getData1(@Param("id") int id);

    @Select("select sum(`like`) as likes,sum(collect) as collects,sum(view) as views from document where document_course = #{course_id}")
    LikeCollectViewDtoR getData2(@Param("course_id") int course_id);

    @Select("select sum(`like`) as likes,sum(collect) as collects,sum(view) as views from course_doc where course_teacher = #{id}")
    LikeCollectViewDtoR getData3(@Param("id") int id);

    @Select("select count(*) from document where document_course = #{id} and is_outline = 1")
    int getOutLine(@Param("id") int course_id);

    @Select("select count(*) from document where document_course = #{id} and is_plan = 1")
    int getPlan(@Param("id") int course_id);

    @Delete("delete from document where document_id = #{document_id}")
    void deleteDocument(@Param("document_id") int document_id);

    void updateDocument(@Param("document") Document document);

    void updateLikeCollectView(@Param("messages") List<DocumentLikeCollectViewDto> likeCollectViewData);
}
